package com.javaspeak.algorithm.sort.quicksort;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

public class QuickSorterTest {

    public Logger logger = Logger.getLogger( QuickSorterTest.class );


    private String debug( int[] numbers ){

        StringBuilder sb = new StringBuilder();

        for ( int i=0; i<numbers.length; i++ ){

            sb.append( numbers[ i ] ).append( " " );
        }

        return sb.toString();
    }

    @Test
    public void testSort(){

        int[][] numbers =
        {
            //{ 8, 5, 3, 7, 4, 2, 6, 1 }
            { 10, 2, 5, 7, 3, 8, 4, 1, 6, 9 }
            //{ 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
            //{ 19, 18, 17, 16, 15, 6, 1, 12, 11, 10, 9, 8, 7, 14, 5, 4, 3, 2, 13 }
        };

        for ( int i=0; i<numbers.length; i++ ){

            logger.debug( "\nUnsorted: " + debug(  numbers[ i ] ) + "\n" );

            QuickSorter.sort( numbers[ i ] );

            logger.debug( "\nSorted: " + debug(  numbers[ i ] )  + "\n" );

            for ( int j=1; j<numbers[ i ].length; j++ ){

                if ( ! ( numbers[ i ][ j ] >= numbers[ i ][ j - 1 ] ) ){

                    org.junit.Assert.fail( "Numbers are not sorted" );
                }
            }
        }
    }


    public static void main( String args[] ){

        BasicConfigurator.resetConfiguration();
        BasicConfigurator.configure();

        JUnitCore junitCore = new JUnitCore();
        junitCore.addListener( new TextListener( System.out ) );
        junitCore.run( QuickSorterTest.class );
    }
}
